package com.base.library.base

import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.view.*
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.base.library.R
import java.lang.reflect.Field


abstract class BaseDialogFragment<VB : ViewDataBinding> :
    DialogFragment() {
    protected lateinit var mVb: VB
    private var mOnDismissListener: DialogInterface.OnDismissListener? = null

    fun setOnDismissListener(listener: DialogInterface.OnDismissListener?) {
        mOnDismissListener = listener
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        mOnDismissListener?.onDismiss(dialog)
    }

    open fun getDialogStyleId() = R.style.MyDialogStyle

    open fun canceledOnTouchOutside() = true

    open fun getGravity() = Gravity.CENTER
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = Dialog(activity as FragmentActivity, getDialogStyleId())
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE) // 去掉标题栏
        dialog.setCanceledOnTouchOutside(canceledOnTouchOutside()) // 点击外部是否关闭Dialog
        dialog.window?.let {
            it.decorView.setPadding(0, 0, 0, 0)
            it.attributes = it.attributes?.let { attributes ->
                attributes.gravity = getGravity()
                attributes.width = WindowManager.LayoutParams.MATCH_PARENT
                attributes.height = WindowManager.LayoutParams.WRAP_CONTENT
                attributes
            }
        }

        return dialog
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        initViewBinding(inflater)
        return mVb.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    private fun initViewBinding(inflater: LayoutInflater) {
        mVb = DataBindingUtil.inflate(inflater, getLayoutId(), null, false)
    }

    override fun onDestroyView() {
        super.onDestroyView()
    }

    override fun show(manager: FragmentManager, tag: String?) {
        showAllowingStateLoss(manager, tag)
    }

    private fun showAllowingStateLoss(manager: FragmentManager, tag: String?) {
        try {
            val dismissed: Field = DialogFragment::class.java.getDeclaredField("mDismissed")
            dismissed.isAccessible = true
            dismissed.set(this, false)
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }
        try {
            val shown: Field = DialogFragment::class.java.getDeclaredField("mShownByMe")
            shown.isAccessible = true
            shown.set(this, true)
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }
        val ft: FragmentTransaction = manager.beginTransaction()
        ft.add(this, tag)
        ft.commitAllowingStateLoss()
    }

    abstract fun getLayoutId(): Int
}